package com.itic.mall.system.inter.clintmsgs;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.TextMessage;

import com.itic.appbase.applications.sys.syssetting.service.SysSettingService;
import com.itic.appbase.applications.websocket.SystemWebSocketHandler;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.mall.system.inter.msg.websocket.service.WebSocketService;
import com.itic.mall.system.inter.pay.paylog.persistence.model.PayLog;
import com.itic.mall.system.inter.pay.paylog.persistence.model.PayLogExample;
import com.itic.mall.system.inter.pay.paylog.persistence.model.PayLogExample.Criteria;
import com.itic.mall.system.inter.pay.paylog.service.PayLogService;



public class ClintMsgsTimer
     extends BaseController {
    
	@Autowired
	private PayLogService payLogService;
	@Autowired
	private WebSocketService webSocketService;
	@Autowired
	private SysSettingService sysSettingService;
	
	@Bean
    public SystemWebSocketHandler systemWebSocketHandler() {
        return new SystemWebSocketHandler();
    }
    
	/**
	 * 推送支付消息到首页
	 */
	public void pushPayLogMsg(){
		List<String> ids = webSocketService.getPushedIdsByTableName("t_pay_log");
		//查找没有推送过的消息
		PayLogExample example = new PayLogExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andAddOrSubEqualTo("add");
		criteria.andIdNotIn(ids);
		List<PayLog> list = payLogService.list(example);
		if( list.size()>0 ){
			SystemWebSocketHandler socketHandler = systemWebSocketHandler();
			for( PayLog temp : list ){
				StringBuffer buffer = new StringBuffer();
				buffer.append(temp.getUserHeadImg()==null?sysSettingService.getByKey("DEFAULT_HEAD_IMAGE"):temp.getUserHeadImg());
				buffer.append(",");
				buffer.append(temp.getUserName());
				buffer.append("在友惠家里赚了");
				buffer.append(temp.getPayMoney());
				buffer.append("元钱！");
				buffer.append(",");
				buffer.append(temp.getPayTime());
				TextMessage message = new TextMessage(buffer.toString());
				socketHandler.sendMessageToUsers(message);
				
			}
		}
	}
	
}